/**
 * 
 */
package com.ruanyun.tio.net.server;

import com.ruanyun.tio.net.server.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.core.intf.Packet;

/**
 * @author tanyaowu
 *  用户根据情况来完成该类的实现，也可以不用实现
 */
public class ServerAioListener implements org.tio.server.intf.ServerAioListener {
	private static Logger log = LoggerFactory.getLogger(ServerAioListener.class);

	public static final ServerAioListener me = new ServerAioListener();

	private ServerAioListener() {

	}

	@Override
	public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect) throws Exception {
//		if (log.isInfoEnabled()) {
//			log.info("onAfterConnected\r\n{}", channelContext);
//		}
//		System.out.println("111111111111111111111111");
		Utils.setSharebikeSessionContext(channelContext);
	}

	@Override
	public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess) throws Exception {
//		if (log.isInfoEnabled()) {
//			log.info("onAfterSent\r\n{}\r\n{}", packet.logstr(), channelContext);
//		}
//		PacketVo sharebikePacket = (PacketVo) packet;
//		byte [] src = sharebikePacket.getBody();
//
//		String hexStr= BinaryHexUtil.binaryToHex(src);
//		System.out.println("成功进入发送回调函数,时间：" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + "指令：" + hexStr);
	}

	@Override
	public void onBeforeClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) {

//		if (log.isInfoEnabled()) {
//			log.info("onBeforeClose\r\n{}", channelContext);
//		}
		System.out.println("心跳超时，解除绑定");
		Tio.close(channelContext,"心跳超时");
	}

	@Override
	public void onAfterDecoded(ChannelContext channelContext, Packet packet, int packetSize) throws Exception {
//		if (log.isInfoEnabled()) {
//			log.info("onAfterDecoded\r\n{}\r\n{}", packet.logstr(), channelContext);
//		}
//		PacketVo sharebikePacket = (PacketVo) packet;
//		byte [] src = sharebikePacket.getBody();
//
//		String hexStr= BinaryHexUtil.binaryToHex(src);
//
//		String electricBicyleCode = hexStr.substring(16,32);
//		String headerCode = hexStr.substring(0,4);
//		if (headerCode.equals("fe9e")) {
//			ChannelContext channelContext1 = Tio.getChannelContextByBsId(channelContext.getGroupContext(), electricBicyleCode);
//			if (null == channelContext1) {
//				Tio.bindBsId(channelContext, electricBicyleCode);
//			}
//		} else if (headerCode.equals("fe8e")) {
//			String userOid = BinaryHexUtil.hexToString(hexStr.substring(32,96));
//			ChannelContext channelContext1 = Tio.getChannelContextByBsId(channelContext.getGroupContext(), userOid);
//			if (null == channelContext1) {
//				Tio.bindBsId(channelContext, userOid);
//			}
//		}
//		SetWithLock<ChannelContext> setChannelContext = Tio.getChannelContextsByUserid(channelContext.getGroupContext(), electricBicyleCode);
//		if (null == setChannelContext && headerCode.equals("fe9e")) {
//			Tio.bindUser(channelContext, electricBicyleCode);
//		} else if (null == setChannelContext && headerCode.equals("fe8e")) {
//			String userOid = BinaryHexUtil.hexToString(hexStr.substring(32,96));
//			Tio.bindUser(channelContext, userOid);
//		}
	}

	@Override
	public void onAfterReceivedBytes(ChannelContext channelContext, int receivedBytes) throws Exception {
//		if (log.isInfoEnabled()) {
//			log.info("onAfterReceivedBytes\r\n{}", channelContext);
//		}
//		System.out.println("55555555555555555555");
	}

	@Override
	public void onAfterHandled(ChannelContext channelContext, Packet packet, long cost) throws Exception {
//		if (log.isInfoEnabled()) {
//			log.info("onAfterHandled\r\n{}\r\n{}", packet.logstr(), channelContext);
//		}
//		System.out.println("66666666666666666666");
	}

	@Override
	public boolean onHeartbeatTimeout(ChannelContext channelContext, Long aLong, int i) {
		return false;
	}
}
